package com.ly.blog2Common.utils;

import org.apache.poi.ss.formula.functions.T;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;

/**
 * 对象化  //包含集成对象
 * @author: lyMaster
 * @time: 2020/8/5 13:38
 */
public class ObjectUtils {

    public static <T> T mapToObject(Map<String,Object> map,Class<T> clazz) throws IllegalAccessException, InstantiationException {
        T t = clazz.newInstance();
        for (; clazz != Object.class; clazz = (Class<T>) clazz.getSuperclass()) {//向上循环  遍历父类
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {

                int mod = field.getModifiers();
                if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
                    continue;
                }
                field.setAccessible(true);
                Object fieldObject = map.get(field.getName());

                //字符串类型
                field.set(t,fieldObject);

            }
        }

        return t;
    }

}
